package com.soujiayi.share;


import java.io.File;

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;

import com.soujiayi.util.Constants;
import com.tencent.weibo.api.TAPI;
import com.tencent.weibo.constants.OAuthConstants;
import com.tencent.weibo.oauthv2.OAuthV2;

public class TecentWeiboAPI{
	
    private OAuthV2 oAuth;
    private Handler handler;
	
	public TecentWeiboAPI(OAuthV2 oAuth, Handler handler) {
		this.oAuth = oAuth;
		this.handler = handler;
	}
	
//	public void send(final String content){
//		new Thread(new Runnable() {
//			@Override
//			public void run() {
//	            try {
//	                TAPI tAPI = new TAPI(OAuthConstants.OAUTH_VERSION_2_A);
//	        		String picPath = Environment.getExternalStorageDirectory() +  File.separator + Constants.STORAGECARD_DIR + File.separator + "logo_icon.png";
//					String response = tAPI.addPic(oAuth,  "json",  content, null, picPath);
//					tAPI.shutdownConnection();
//					
//					Message msg = new Message();
//					Bundle bundle = new Bundle();
//					bundle.putInt("api_type", 2);
//					bundle.putInt("function_type", 2);
//					bundle.putBoolean("isSuccess", true);
//					msg.setData(bundle);
//					handler.sendMessage(msg);
//				} catch (Exception e) {
//					System.out.println("Tencent->" + e);
//					Message msg = new Message();
//					Bundle bundle = new Bundle();
//					bundle.putInt("api_type", 2);
//					bundle.putInt("function_type", 2);
//					bundle.putBoolean("isSuccess", false);
//					msg.setData(bundle);
//					handler.sendMessage(msg);
//				}
//			}
//		}).start();
//	}
	
	public void addPic(final String content, final String imageUrl){
//		boolean isImageExist = false;
//		String picPath = "";
//		if(imageUrl != null && !imageUrl.equals("")){
//			picPath = Environment.getExternalStorageDirectory() +  File.separator + Constants.CACHE_DIR + File.separator + String.valueOf(imageUrl.hashCode());
//			File f = new File(picPath);
//			if(f.exists()){
//				isImageExist = true;
//			}
//		}
//		if(!isImageExist){
//			add(content);
//			return;
//		}
//		
		new Thread(new Runnable() {
			@Override
			public void run() {
	            try {
	                TAPI tAPI = new TAPI(OAuthConstants.OAUTH_VERSION_2_A);
//	                String picPath = Environment.getExternalStorageDirectory() +  File.separator + Constants.CACHE_DIR + File.separator + String.valueOf(imageUrl.hashCode());
					String response = tAPI.addPic(oAuth,  "json",  content, null, imageUrl);
					tAPI.shutdownConnection();
					
					Message msg = new Message();
					Bundle bundle = new Bundle();
					bundle.putInt("api_type", 2);
					bundle.putInt("function_type", 2);
					bundle.putBoolean("isSuccess", true);
					msg.setData(bundle);
					handler.sendMessage(msg);
				} catch (Exception e) {
					System.out.println("Tencent->" + e);
					Message msg = new Message();
					Bundle bundle = new Bundle();
					bundle.putInt("api_type", 2);
					bundle.putInt("function_type", 2);
					bundle.putBoolean("isSuccess", false);
					msg.setData(bundle);
					handler.sendMessage(msg);
				}
			}
		}).start();
	}
	
	public void add(final String content){
		new Thread(new Runnable() {
			@Override
			public void run() {
	            try {
	                TAPI tAPI = new TAPI(OAuthConstants.OAUTH_VERSION_2_A);
					String response = tAPI.add(oAuth,  "json",  content, null);
					tAPI.shutdownConnection();
					
					Message msg = new Message();
					Bundle bundle = new Bundle();
					bundle.putInt("api_type", 2);
					bundle.putInt("function_type", 2);
					bundle.putBoolean("isSuccess", true);
					msg.setData(bundle);
					handler.sendMessage(msg);
				} catch (Exception e) {
					System.out.println("Tencent->" + e);
					Message msg = new Message();
					Bundle bundle = new Bundle();
					bundle.putInt("api_type", 2);
					bundle.putInt("function_type", 2);
					bundle.putBoolean("isSuccess", false);
					msg.setData(bundle);
					handler.sendMessage(msg);
				}
			}
		}).start();
	}
	


}
